#include<bits/stdc++.h>
#define ll long long
#define L xd[x].l
#define R xd[x].r
#define mid ((l+r)>>1)
#define lc L,l,mid
#define rc R,mid+1,r
#define Root 1,1,n-1
#define OK Ll<=l&&r<=Rr
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define repn(x) rep(x,1,n)
#define repm(x) rep(x,1,m)
#define pb push_back
#define e(x) for(int i=h[x],y=to[i];i;i=nxt[i],y=to[i])
#define E(x) for(auto y:p[x])
#define Pi pair<int,int>
#define ui unsigned ll
inline int read(){int s=0,w=1;char c=getchar();while(c<48||c>57) {if(c=='-') w=-1;c=getchar();}while(c>=48&&c<=57)s=(s<<1)+(s<<3)+c-48,c=getchar();return s*w;}
inline void pf(ll x){if(x<0) putchar('-'),x=-x;if(x>9)pf(x/10);putchar(x%10+48);}
using namespace std;
const int N=2e5+5,M=3.3e4+5,inf=(1LL<<29)-1,mod=998244353;
const ll llf=1e18;
inline void add(int &a,int b){((a+=b)>=mod) and (a-=mod);}
inline int Add(int a,int b){return add(a,b),a;}
inline int mul(int a,int b){return 1LL*a*b%mod;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline void red(int &a,int b){add(a,mod-b);}
inline int Red(int a,int b){return red(a,b),a;}
inline int qp(int a,ll b){if(!b)return 1;int c=qp(a,b>>1LL);Mul(c,c);if(b&1)Mul(c,a);return c;}
inline int INV(int x){return qp(x,mod-2);}
bool St;
mt19937 rd(time(0));
int n,m,c,a[N],h[N],to[N<<1],nxt[N<<1],cnt,S;
inline void add_(int a,int b){
	to[++cnt]=b,nxt[cnt]=h[a],h[a]=cnt;
}
int Sz[N];
const int B=700;
int st[N],tp,F[B][M],tag[N],f[M],val[N];
inline void Dfs(int x,int fa){
	e(x)if(y^fa)val[y]=val[x]|(1<<a[x])|(1<<a[y]),Dfs(y,x);
}
inline void solve(int x){
	rep(i,0,S)f[i]=0;
	val[x]=0;
	e(x)val[y]=val[x]|(1<<a[x])|(1<<a[y]),Dfs(y,x);
	repn(i)f[val[i]]++;
	rep(j,0,c-1)
		rep(i,0,S)if((i>>j)&1)f[i]+=f[i^(1<<j)];
	rep(i,0,S)F[tag[x]][i]=f[i];
}
inline void dfs(int x,int fa){
	Sz[x]=1;
	e(x)if(y^fa)dfs(y,x),Sz[x]+=Sz[y];
	if(Sz[x]>=B){
		Sz[x]=0,st[tag[x]=++tp]=x;
		solve(x);
	}
}
bool fl;
inline void dfs_find(int x,int fa,int s,int &c){
	if(tag[x])return c=F[tag[x]][s],fl=1,void();
	c++;
	e(x)if(y^fa&&((s>>a[y])&1)){
		dfs_find(y,x,s,c);
		if(fl)return;
	}
}
inline void Main(){
	n=read(),m=read(),c=read(),S=(1<<c)-1;
	repn(i)a[i]=read();
	rep(i,2,n){
		int x=read();
		add_(x,i),add_(i,x);
	}
	dfs(1,0);
	repm(i){
		int s=read(),x=read();
		if(!((s>>a[x])&1))puts("1");
		else {
			fl=0;
			int ans=0;
			dfs_find(x,0,s,ans);
			cout<<ans<<'\n';
		}
	}
}
bool Ed;
signed main(){
	// cerr<<"[Memory]"<<(&St-&Ed)/1024/1024<<"MB."<<'\n';
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	int T=1;
	while(T--)Main();
	return 0;
}